import { createRouter, createWebHistory } from 'vue-router'
import routes from './routes'
import { get_token } from '@/utils/auth'
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import {check_login} from '@/api/login'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

router.beforeEach(async (to,from,next)=>{
  NProgress.start();
  if (!to.meta.not_check) {
    if (!get_token()) {
      next('login');
      return;
    }
    var res=await check_login();
    console.log(res);
    if(res.code!==0){
      next('/login');
      return;
    }
    sessionStorage.setItem("user",JSON.stringify(res.data));
  }
  next();
});
router.afterEach(()=>{
  NProgress.done();
})
export default router
